﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

namespace Defilade
{
    public partial class ExceptionDialog : Form
    {
        private ExceptionDialog()
        {
            InitializeComponent();
        }

        public ExceptionDialog(Exception ex) : this()
        {
            exceptionNameTextBox.Text = ex.Message;
            
            //Build a readable message
            StringBuilder builder = new StringBuilder();
            builder.AppendLine(ex.Message);
            builder.AppendLine(String.Empty);
            builder.AppendLine("Stack Trace:");
            builder.AppendLine("----- START STACK TRACE -----");
            builder.AppendLine(ex.StackTrace);
            builder.AppendLine("----- END STACK TRACE -----");
            builder.AppendLine(String.Empty);
            builder.AppendLine("Inner Exception:");
            if (ex.InnerException != null)
            {
                builder.AppendLine(ex.InnerException.Message);
                builder.AppendLine(String.Empty);
                builder.AppendLine("Stack Trace:");
                builder.AppendLine("----- START INNER STACK TRACE -----");
                builder.AppendLine(ex.InnerException.StackTrace);
                builder.AppendLine("----- END INNER STACK TRACE -----");
            }
            else
            {
                builder.AppendLine("None");
            }
            builder.AppendLine(String.Empty);

            stackTraceTextWindow.Text = builder.ToString();
        }

        private void ExceptionDialog_Load(object sender, EventArgs e)
        {
            reportErrorLink.Links.Remove(reportErrorLink.Links[0]);
            reportErrorLink.Links.Add(0, reportErrorLink.Text.Length, "http://code.google.com/p/defilade/issues/list");
        }

        private void reportErrorLink_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            ProcessStartInfo sInfo = new ProcessStartInfo(e.Link.LinkData.ToString());
            Process.Start(sInfo);
        }
    }
}
